package cn.kgc.springbootdemo.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.kgc.springbootdemo.entity.Role;
import cn.kgc.springbootdemo.entity.RoleMenu;
import cn.kgc.springbootdemo.form.PermissionsFprm;
import cn.kgc.springbootdemo.form.RolePageForm;
import cn.kgc.springbootdemo.service.IRoleService;
import cn.kgc.springbootdemo.service.RoleMenuService;
import cn.kgc.springbootdemo.util.BasePage;
import cn.kgc.springbootdemo.util.R;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DongZeJian
 * @since 2020-06-23
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色前端控制器")
@RequiredArgsConstructor
@Validated
@Slf4j
public class RoleController {
    private final IRoleService roleService;
    private final RoleMenuService roleMenuService;

    @PostMapping("/page")
    @ApiOperation("角色分页查询")
    public R<Page<Role>> page(@RequestBody @Valid RolePageForm rolePageForm){
        return R.ok(roleService.page(rolePageForm.toPage(), Wrappers.<Role>lambdaQuery().like(StrUtil.isNotBlank(rolePageForm.getRoleName()),Role::getRoleName,rolePageForm.getRoleName())));
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除角色")
    public R<Boolean> delete(@PathVariable String id){
        return R.ok(roleService.removeById(id));
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加或更新")
    public R<Boolean> addOrUpdate(@RequestBody Role role){
        return R.ok(roleService.saveOrUpdate(role));
    }

    @GetMapping("/permissionsInfo/{roleId}")
    @ApiOperation("角色权限回显")
    public R<List<Integer>> permissionsInfo(@PathVariable String roleId){
        List<RoleMenu> roleMenus = roleMenuService.list(Wrappers.<RoleMenu>lambdaQuery().eq(RoleMenu::getRoleId, roleId));
        if (CollUtil.isEmpty(roleMenus)){
            return null;
        }
        return R.ok(roleMenus.stream()
                    .map(RoleMenu::getMenuId)
                    .collect(Collectors.toList()));
    }

    @PostMapping("/permissions")
    @ApiOperation("权限")
    public R<Boolean> permissions(@RequestBody @Valid PermissionsFprm permissionsFprm){
        //先删除
        roleMenuService.remove(Wrappers.<RoleMenu>lambdaQuery().eq(RoleMenu::getRoleId,permissionsFprm.getRoleId()));

        //在批量添加
        List<RoleMenu> roleMenus= Lists.newArrayList();
        for (Integer menuId : permissionsFprm.getMenuId()) {
            roleMenus.add(new RoleMenu(permissionsFprm.getRoleId(),menuId));
        }
        return R.ok(roleMenuService.saveBatch(roleMenus));
    }
}

